// 基本计算器

package Leetcode;

import java.util.Deque;
import java.util.LinkedList;

class Solution_227_1 {
    public int calculate(String s) {
        s = s.replaceAll("\\s+", "");
        String[] strnums = s.split("[+-/*/%]");
        int[] nums = new int[strnums.length];
        for (int i = 0; i < strnums.length; i++) {
            String str = strnums[i];
            int sum = 0;
            for (int j = 0; j < str.length(); j++) {
                sum = sum * 10 + (str.charAt(j) - '0');
            }
            nums[i] = sum;
        }

        Deque<Integer> num = new LinkedList<>();
        Deque<Character> operator = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') // 空格
                continue;
            else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') // 数字
                num.addLast(s.charAt(i) - '0');
            else { // 操作符
                if (operator.isEmpty()) { // 如果栈为空
                    operator.addLast(s.charAt(i));
                    continue;
                }
                // 大于等于当前运算符优先级要出栈
                char c = operator.getLast();
                if (c == '*') {
                    operator.removeLast();
                    int num2 = num.removeLast();
                    int num1 = num.removeLast();
                    num.addLast(num1 * num2);
                } else if (c == '/') {
                    operator.removeLast();
                    int num2 = num.removeLast();
                    int num1 = num.removeLast();
                    num.addLast(num1 / num2);
                } else if (c == '%') {
                    operator.removeLast();
                    int num2 = num.removeLast();
                    int num1 = num.removeLast();
                    num.addLast(num1 % num2);
                }
                operator.addLast(s.charAt(i));
            }
        }
        while (!operator.isEmpty()) {
            char c = operator.removeLast();
            if (c == '*') {
                int num2 = num.removeLast();
                int num1 = num.removeLast();
                num.addLast(num1 * num2);
            } else if (c == '/') {
                int num2 = num.removeLast();
                int num1 = num.removeLast();
                num.addLast(num1 / num2);
            } else if (c == '%') {
                int num2 = num.removeLast();
                int num1 = num.removeLast();
                num.addLast(num1 % num2);
            } else if (c == '-') {
                int num2 = num.removeLast();
                int num1 = num.removeLast();
                num.addLast(num1 - num2);
            } else {
                int num2 = num.removeLast();
                int num1 = num.removeLast();
                num.addLast(num1 + num2);
            }
        }

        return num.removeLast();
    }
}

class Solution_227 {
    public int calculate(String s) {
        s = s.replaceAll("\\s+", "");
        char operator = '+'; // 记录num的前一个符号
        int num = 0;
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                num = num * 10 + s.charAt(i) - '0';
            }
            if (!Character.isDigit(s.charAt(i)) || i == s.length() - 1) { // 当遇到num的下一个符号时,或者到达最后
                switch (operator) {
                    case '+':
                        stack.push(num);
                        break;
                    case '-':
                        stack.push(-num);
                        break;
                    case '*':
                        stack.push(stack.pop() * num);
                        break;
                    default:
                        stack.push(stack.pop() / num);
                        break;
                }
                operator = s.charAt(i); // 变为后一个操作符
                num = 0;
            }
        }

        int result = 0;
        while (!stack.isEmpty()) {
            result += stack.pop();
        }
        return result;
    }
}